package com.android.RssReader.Screen;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.RssReader.R;
import com.android.RssReader.RssReaderApplication;
import com.android.RssReader.View.DraggableListView;


public class ScreenCategory extends Screen{

	public ScreenCategory() {
		super(SCREEN_TYPE.SCREEN_CATEGORYS_T, ScreenCategory.class.getCanonicalName());
	}

	private static String[] items = { "浙江", "江苏", "广东", "上海", "北京", "新疆" };
	private IconicAdapter adapter = null;
	private ArrayList<String> array = new ArrayList<String>(
			Arrays.asList(items));
	DraggableListView mListView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		RssReaderApplication.LogD(ScreenCategory.class, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_categoty);
		 mListView = (DraggableListView) findViewById(R.id.draggable_list);    
		 adapter = new IconicAdapter();
		 mListView.setAdapter(adapter);

			mListView.setDropListener(onDrop);
			mListView.setRemoveListener(onRemove);
			mListView.getAdapter();
		}

	private DraggableListView.DropListener onDrop = new DraggableListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			String item = adapter.getItem(from);

			adapter.remove(item);
			adapter.insert(item, to);
			Toast.makeText(ScreenCategory.this, adapter.getList().toString(),
					Toast.LENGTH_SHORT).show();
		}
	};

	private DraggableListView.RemoveListener onRemove = new DraggableListView.RemoveListener() {
		@Override
		public void remove(int which) {
			adapter.remove(adapter.getItem(which));
		}
	};

	class IconicAdapter extends ArrayAdapter<String> {
		IconicAdapter() {
			super(ScreenCategory.this, R.layout.screen_category_list_item, array);
		}

		public ArrayList<String> getList() {
			return array;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.screen_category_list_item, parent, false);
			}
			TextView label = (TextView) row.findViewById(R.id.label);
			label.setText(array.get(position));
			return (row);
		}
	}
}